import Vue from "vue";
import axios from "axios";
import { message } from "ant-design-vue";

export const deepCopy = function (obj) {
    let _obj = Array.isArray(obj) ? [] : {}
    for (let i in obj) {
        _obj[i] = typeof obj[i] === 'object' ? deepCopy(obj[i]) : obj[i]
    }
    return _obj
};

export const delIdData = function (object, id) {
    if (Array.isArray(object) === false) {
        return object
    } else {
        let l = object.length;
        for (let i = l; i--; i >= 0) {
            if (typeof object[i] === 'object') {
                if (object[i].data.id === id) {
                    object.splice(i, 1);
                    continue
                }
                if (object[i].data.components !== undefined) {
                    object[i].data.components = delIdData(object[i].data.components, id)
                }
            }
        }
        return object
    }
};

Vue.prototype.$post = (url, params) => {
    return new Promise(function (resolve, reject) {
        axios.post(url, params).then((response) => {
            const data = response.data;
            if (data.err === false) {
                resolve(data.data)
            }else{
                message.warning(data.msg)
                reject(data.code, data.msg, data.data)
            }
        }).catch((error) => {
            message.warning("网络请求失败, 请稍后再试")
            reject(error)
        })
    });
};
Vue.prototype.$deepCopy = deepCopy